from api_server.api_recruit import ApiRecruit
from crawler_core.constant.constants import Constants
from crawler_core.utils.redis_utils import RedisUtils
from lib.sys.logger_factory import LoggerFactory


class TaskRouter(object):
    logger = LoggerFactory.getLogger("TaskRouter")

    def __init__(self):
        pass

    def start_download(self, data, task_id):
        context = data.get(Constants.API_PARAM_CONTEXT)
        context[Constants.CONTEXT_EVENT_TYPE] = Constants.EVENT_TYPE_DOWNLOAD
        context[Constants.CONTEXT_TYPE] = data.get(Constants.API_PARAM_SITE_CODE)
        context[Constants.CONTEXT_TENANT_ID] = data.get(Constants.API_PARAM_TENANT_ID)
        context[Constants.CONTEXT_CORP_CODE] = data.get(Constants.API_PARAM_CORP_CODE)
        channel = data.get(Constants.API_PARAM_SITE_CODE)
        corp_code = data.get(Constants.API_PARAM_CORP_CODE)
        # 获取账号
        TaskRouter.logger.info("服务获取查询任务 --- 获取账号， 渠道--[%s]--,企业--[%s]--" % (channel, corp_code))
        _list_account = ApiRecruit.get_account(corp_code, channel)
        TaskRouter.logger.info("服务获取查询任务 --- 获取账号， 账号数据为 {}".format(_list_account))
        # 多账号问题以后处理
        if _list_account:
            account_json = _list_account[0]
            if account_json:
                context[Constants.CONTEXT_USERNAME] = account_json.get(Constants.CONTEXT_USERNAME)
                context[Constants.CONTEXT_PASSWORD] = account_json.get(Constants.CONTEXT_PASSWORD)
                context[Constants.CONTEXT_ACCOUNT] = account_json.get(Constants.CONTEXT_ACCOUNT)
            else:
                return
            context[Constants.CONTEXT_ACCOUNT_LIST] = _list_account
            # 保存任务数据
            RedisUtils.setEventTask(task_id, context)
            # 发送登录命令
            RedisUtils.setLoginTask(task_id)
            TaskRouter.logger.info("服务获取查询任务 -- [%s] --，发送登录指令" % task_id)

    def start_search_second(self, data, task_id):
        cur_page = int(data[Constants.CONTEXT_CUR_PAGE])
        # print(cur_page)
        size = data[Constants.CONTEXT_PAGE_SIZE]
        number = self.get_page(data[Constants.CONTEXT_TOTAL], data[Constants.CONTEXT_CUR_TOTAL], size)
        _list = []
        for page in range(1, number + 1):
            context = {
                Constants.CONTEXT_ROOT_TASK_ID: task_id,
                Constants.CONTEXT_EVENT_TYPE: data[Constants.CONTEXT_EVENT_TYPE],
                Constants.CONTEXT_TYPE: data[Constants.CONTEXT_TYPE],
                Constants.CONTEXT_CORP_CODE: data[Constants.CONTEXT_CORP_CODE],
                Constants.CONTEXT_SEARCH_CONDITION: data[Constants.CONTEXT_SEARCH_CONDITION],
                Constants.CONTEXT_ACCOUNT: data[Constants.CONTEXT_ACCOUNT],
                Constants.CONTEXT_USERNAME: data[Constants.CONTEXT_USERNAME],
                Constants.CONTEXT_PASSWORD: data[Constants.CONTEXT_PASSWORD],
                Constants.CONTEXT_ACCOUNT_LIST: data[Constants.CONTEXT_ACCOUNT_LIST],
                Constants.CONTEXT_CUR_PAGE: cur_page,
                Constants.CONTEXT_PAGE_SIZE: size
            }
            #  完成分片数据
            card_task_id = str(task_id) + "_" + str(cur_page)
            cur_page += 1
            _obj = {card_task_id: context}
            _list.append(_obj)

        # 上报任务分片
        root_context = RedisUtils.getEventSearchContext(task_id)
        root_context[Constants.CONTEXT_CARD_NUMBER] = len(_list)
        RedisUtils.setEventSearchContext(task_id, root_context)

        for obj in _list:
            for key, values in obj.items():
                RedisUtils.setEventTask(key, values)
                # 发送登录命令
                RedisUtils.setLoginTask(key)
                TaskRouter.logger.info("服务获取查询任务 -- [%s] --，发送登录指令" % key)

    def get_page(self, total, cur_total, page_size):
        _number = int(total) - int(cur_total)
        f_number = _number / page_size
        number = int(_number / page_size)
        if f_number - number > 0:
            number += 1
        return number
